﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading;

namespace S7.Windows.Menu
{
    /// <summary>
    /// Interaction logic for ShowMenu.xaml
    /// </summary>
    public partial class ShowMenu : Window
    {
        public ShowMenu()
        {
            InitializeComponent();
        }

        private void lbCategories_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string selectedMenu = lbCategories.SelectedItem.ToString();
            try
            {
                if (MenuItem.lstItems != null)
                {
                    if (selectedMenu == "All")
                    {
                        try
                        {
                            lbMenuItems.ItemsSource = MenuItem.lstItems;
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
                        }
                    }
                    else
                    {
                        var query = from menu in Menus.lstMenus
                                    where menu.MenuName == selectedMenu
                                    select menu;
                        List<Menus> SelectedMenu = query.ToList();
                        int currentMenuID = SelectedMenu[0].MenuID;

                        var items = from item in MenuItem.lstItems
                                    where item.MenuID == currentMenuID
                                    select item;
                        try
                        {
                            lbMenuItems.ItemsSource = items;
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
                        }
                    }
                }
                else
                {
                    Thread.Sleep(100);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                Menus.lstMenus = Menus.Search().ToList();
                try
                {
                    lbCategories.Items.Add("All");
                    foreach (var item in Menus.lstMenus)
                    {
                        lbCategories.Items.Add(item.MenuName);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
                }

                MenuItem.lstItems = MenuItem.Search();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Menus.lstMenus = null;
            MenuItem.lstItems = null;
        }

        private void tbSearch_KeyUp(object sender, KeyEventArgs e)
        {
            string text = tbSearch.Text;
            if (text == "")
            {
                lbMenuItems.ItemsSource = MenuItem.lstItems;
            }
            else
            {
                while (true)
                {
                    if (MenuItem.lstItems == null)
                    {
                        Thread.Sleep(50);
                    }
                    else
                    {
                        List<MenuItem> lstItems = MenuItem.TextSearch(text);
                        lbMenuItems.ItemsSource = lstItems;
                        break;
                    }
                }
            }
        }
    }
}
